/**
 * 
 * JShakki  - Dude with recycled paper.
 * 
 * @version 1.0
 * 
 * Copyright (C) 2006 Jarkko Sipilä
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * 
 */

package dude.gui;
import javax.swing.*; 

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;

public class BoardComponent extends JPanel {
  
  private static final long serialVersionUID = -7533451324150251648L;

  private SquareComponent[][] squares;
  private DudeComponent dudeData;
  
  
  protected BoardComponent( int lineSize, DudeComponent creator ) {
    
    this.dudeData = creator;
    this.squares = new SquareComponent[lineSize][lineSize];
    
    this.setLayout( new GridLayout(lineSize, lineSize) );
        
    for ( int y = 0; y < lineSize; y++ ) {
      for ( int x = 0; x < lineSize; x++ ) {
        SquareComponent c = new SquareComponent( new Dimension(x, y) );
        this.add( c );
        this.squares[x][y] = c;
        
        c.addMouseListener( new MouseAdapter() {
          public void mousePressed( MouseEvent e ) {
            squareMousePressed( e );
          }
        });	
      }
    }
  }
  
  protected void drawLine( Dimension d, int direction ) {
    this.squares[d.width][d.height].drawLine(direction);
  }
  
  protected void drawX( Dimension d ) {
    this.squares[d.width][d.height].drawX();
  }
  
  protected void drawO( Dimension d ) {
    this.squares[d.width][d.height].drawO();		
  }
  
  
  private void squareMousePressed( MouseEvent e ) {
    SquareComponent r = (SquareComponent)e.getComponent();
    Dimension d = r.getCoordinates();
    
    this.dudeData.dataSetOwner(d); 
  }
}
